function [outputresultmap outputresulterrormap] = kneeregister(fixedstudydir, fixedfile, movingstudydir, movingfile)
% ratheartregister: run registration between multiple days
%
% Usage:  outputresultmap = ratheartregister(fixedstudydir, movingstudydir)
%
% Where: 
%   fixedstudydir, movingstudydir are the T2 directories of day 1, and
% a next day, respectively.
%   outputresultmap = the name of the registered (moving) R2map dcm.
%

% remove slashes at the end: fixed
while ( fixedstudydir(end) == '/' || fixedstudydir(end) == '\' )
    fixedstudydir = fixedstudydir(1:(end-1));
end
% output dir
fixeddir = [ fixedstudydir 'regis\' ];
if exist(fixeddir)~=7
   mkdir(fixeddir);
end

% remove slashes at the end: moving
while ( movingstudydir(end) == '/' || movingstudydir(end) == '\' )
    movingstudydir = movingstudydir(1:(end-1));
end
% output dir
movingdir = [ movingstudydir 'regis\' ];
if exist(movingdir)~=7
   mkdir(movingdir);
end

% find elastix.exe
currentdir= fileparts( which('kneeregister.m') );
elastixdir = fullfile(currentdir, 'elastix');

% define all input arguments for elastix
fixedim = fullfile(fixedfile);
movingim = fullfile(movingfile);

% fixedmask = ratheartmakeregmask(fixedstudydir);
% movingmask = ratheartmakeregmask(movingstudydir);

par = fullfile(elastixdir, 'parameters_RigidLMI.txt');
outdir = fullfile(movingdir);

% finaltp = fullfile(outdir, 'TransformParameters.1.txt');
% movingmap = fullfile(movingdir, 'R2map.dcm');
% movingerrormap = fullfile(movingdir, 'R2errormap.dcm');
% resultmap = fullfile(outdir, 'result.dcm');
% resultTE = fullfile(outdir, 'result.1.dcm');
% outputresultmap = fullfile(movingdir, 'registeredR2map.dcm');
% outputresulterrormap = fullfile(movingdir, 'registeredR2errormap.dcm');
% outputresultTE = fullfile(movingdir, 'registeredfirstTE.dcm');

if ~exist(outdir, 'dir')
  disp( ['making ' outdir]);     
  [success, message, messageid] = mkdir(outdir);
  if ~success    
     error( 'ratheartregister:invalidoutputdir', message );     
  end 
end

% checkfiles = {fixedim, movingim, fixedmask, movingmask, fixedpoints, movingpoints, par1, par2, movingmap};
% for cf = 1:numel(checkfiles)
%   if ~exist(checkfiles{cf})
%       disp( [checkfiles{cf} ' is missing!' ] );
%       error( 'ratheartregister:missinginputfile', 'missing input file!' );
%   end
% end

% call elastix 
% cmdline1 = ['"' fullfile(elastixdir, 'elastix') ...
%   '" -f "' fixedim '" -m "' movingim ...
%   '" -fp "' fixedpoints '" -mp "' movingpoints ...
%   '" -fMask "' fixedmask '" -mMask "' movingmask ...
%   '" -p "' par1 '" -p "' par2 '" -out "' outdir '"'];

cmdline1 = ['"' fullfile(elastixdir, 'elastix') ...
  '" -f "' fixedim '" -m "' movingim ...
  '" -p "' par '" -out "' outdir '"'];

[status1,result1] = system(cmdline1);

% % check if elastix was successful
% checkfiles = {finaltp};
% for cf = 1:numel(checkfiles)
%   if ~exist(checkfiles{cf})
%       disp( [checkfiles{cf} ' is missing!' ] );
%       disp( result1 );
%       error( 'ratheartregister:missingoutputfile', 'missing output file from elastix!' );      
%   end
% end
% 
% % copy the result.1.mhd to outputresultTE
% copyfile( resultTE, outputresultTE );
% 
% % call transformix, to transform the R2map
% cmdline2 = ['"' fullfile(elastixdir, 'transformix') '" -in "' movingmap '" -out "' outdir '" -tp "' finaltp '"'];
% [status2,result2] = system(cmdline2);
% 
% % copy the result to outputresultmap
% copyfile( resultmap, outputresultmap );
% 
% % call transformix, to transform the R2errormap
% cmdline2 = ['"' fullfile(elastixdir, 'transformix') '" -in "' movingerrormap '" -out "' outdir '" -tp "' finaltp '"'];
% [status2,result2] = system(cmdline2);
% 
% % copy the result to outputresultmap
% copyfile( resultmap, outputresulterrormap );


